package com.twotwoshop.service.order.config;

import io.lettuce.core.ReadFrom;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redission配置
 */
@Configuration
public class RedissonConfig {



    /**
     * 开启redis集群的读写分离 优先从从节点读 主节点写
     * @return
     */
    @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer() {
        return clientConfigurationBuilder -> {
            //设置从节点读
            clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
        };
    }


    @Bean
    public RedissonClient redissonClient() {
        //配置类
        Config config = new Config();
        //添加Redis地址 添加单节点地址 也可以使用config,userClusterServers（）添加集群地址
        config.useSingleServer().setAddress("redis://192.168.25.136:6379");
        //创建客户端
        return Redisson.create(config);
    }
}
